﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _09_ListBox控件
{
    public partial class Form1 : Form
    {
        //坐标编号
        int index = 1;
        //随机数对象
        Random rd = new Random();

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 添加坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //拼接坐标字符串
            string str = string.Format("P{0}:({1},{2})", index++,rd.Next(0, 100), rd.Next(0, 100));
            //添加坐标
            listBox1.Items.Add(str);
        }
        /// <summary>
        /// 删除坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //获取选中的项
            int index = listBox1.SelectedIndex;
            //判断是否选中
            if (index != -1)//选中的索引不为-1
            {
                //删除选中的项
                listBox1.Items.RemoveAt(index);
            }
            else
            {
                MessageBox.Show("请选择要删除的项","温馨提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
            }
        }
        /// <summary>
        /// 置顶
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = this.listBox1.SelectedIndex; i >0; i--)
            {
                string SelectStr = this.listBox1.SelectedItem.ToString();//获取选中的文本
                string UpStr = this.listBox1.Items[i - 1].ToString();//获取上一个文本
                //两者之间交换 
                this.listBox1.Items[i - 1] = SelectStr;
                this.listBox1.Items[i] = UpStr;
                //设置选中的索引
                this.listBox1.SelectedIndex = i - 1;
            }

        }
        /// <summary>
        /// 上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            //判断是否有选中的项目，也就是索引不为-1
            int index = listBox1.SelectedIndex;
            if (index ==-1)
            {
                MessageBox.Show("请选择要移动的项", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            }
            //判断索引的值
            if (index > 0)
            {
                string SelectStr = this.listBox1.SelectedItem.ToString();//获取选中的文本
                string UpStr = this.listBox1.Items[index - 1].ToString();//获取上一个文本
                //两者之间交换
                this.listBox1.Items[index - 1] = SelectStr;
                this.listBox1.Items[index] = UpStr;
                //设置选中的索引
                this.listBox1.SelectedIndex = index - 1;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //判断是否有选中对应的项，也就是索引不为-1
            int index = this.listBox1.SelectedIndex;
            if (index == -1)
            {
                MessageBox.Show("请选择要移动的项", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                return;
            }
            if (index < this.listBox1.Items.Count - 1)
            {
                string SelectStr = this.listBox1.SelectedItem.ToString();//获取选中的项
                string UpStr = this.listBox1.Items[index + 1].ToString();//获取下一项
                //两者进行交换
                this.listBox1.Items[index + 1] = SelectStr;
                this.listBox1.Items[index] = UpStr;
                //设置新选中的索引
                this.listBox1.SelectedIndex = index + 1;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            for (int i = this.listBox1.SelectedIndex; i < this.listBox1.Items.Count - 1; i++)
            {
                string SelectStr = this.listBox1.SelectedItem.ToString();//获取选中的项
                string UpStr = this.listBox1.Items[i + 1].ToString();//获取下一项
                //两者进行交换
                this.listBox1.Items[i + 1] = SelectStr;
                this.listBox1.Items[i] = UpStr;
                //设置新选中的索引
                this.listBox1.SelectedIndex = i + 1;
            }
        }
    }
}
